package com.vercon.moensmart.adapter

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.net.wifi.ScanResult
import android.net.wifi.WifiConfiguration
import android.net.wifi.WifiManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.app.ActivityCompat
import com.vercon.moensmart.R
import com.vercon.moensmart.adapter.base.BaseAdapter
import com.vercon.moensmart.databinding.ItemSettingWifiBinding

class SettingWIFIRVAdapter(context: Context) : BaseAdapter<ItemSettingWifiBinding, ScanResult?>(
        context
    ) {
    private val wifiManager: WifiManager
    private var mListener: OnItemLongClickListener? = null

    init {
        wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
    }

    fun setOnItemLongClickListener(longClickListener: OnItemLongClickListener?) {
        mListener = longClickListener
    }

    override fun baseViewHolder(parent: ViewGroup): BaseViewHolder {
        val binding = ItemSettingWifiBinding.inflate(
            LayoutInflater.from(
                context
            ), parent, false
        )
        return ViewHolder(binding)
    }

    override fun bindItemView(viewHolder: BaseViewHolder?, position: Int, scanResult : ScanResult?, total: Int) {
        var name: String = scanResult!!.SSID
        if (name.length > 16) {
            name = name.substring(0, 16) + "..."
        }
        viewHolder?.mBinding?.nameTV?.setText(name)
        // 是否连接过
        if (isExsits(scanResult.SSID) != null) {
            viewHolder?.mBinding?.tvWifiSate?.setVisibility(View.VISIBLE)
        } else {
            viewHolder?.mBinding?.tvWifiSate?.setVisibility(View.GONE)
        }
        // wifi强度
        viewHolder?.mBinding?.wifiStateIV?.setImageResource(
            getResId(
                WifiManager.calculateSignalLevel(
                    scanResult.level,
                    100
                )
            )
        )
        viewHolder!!.itemView.setOnLongClickListener { v: View? ->
            //长按取消保存
            if (mListener != null && isExsits(scanResult.SSID) != null) {
                mListener!!.onItemLongClick(scanResult, viewHolder?.mBinding?.tvWifiSate)
            }
            false
        }
        viewHolder!!.itemView.setOnClickListener {
            mItemListener?.onItemClickListener(it,scanResult,position)
        }
    }


    inner class ViewHolder(itemBinding: ItemSettingWifiBinding) : BaseViewHolder(itemBinding)

    interface OnItemLongClickListener {
        fun onItemLongClick(result: ScanResult?, view: View?)
    }

    fun getResId(level: Int): Int {
        if (level <= 0) {
            return R.drawable.ic_wifi_level_new_1
        } else if (level <= 50) {
            return R.drawable.ic_wifi_level_new_11
        } else if (level <= 70) {
            return R.drawable.ic_wifi_level_new_2
        } else if (level <= 90) {
            return R.drawable.ic_wifi_level_new_3
        } else if (level <= 100) {
            return R.drawable.ic_wifi_level_new_4
        }
        return R.drawable.ic_wifi_level_new_not
    }

    // 查看以前是否也配置过这个网络
    private fun isExsits(SSID: String): WifiConfiguration? {
        if (ActivityCompat.checkSelfPermission(context!!, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return null
        }
        val existingConfigs = wifiManager.configuredNetworks
        if (existingConfigs == null || existingConfigs.size <= 0) {
            return null
        }
        for (existingConfig in existingConfigs) {
            if (existingConfig.SSID == "\"" + SSID + "\"") {
                return existingConfig
            }
        }
        return null
    }
}